package com.nebula.controller.admin;

import com.nebula.dto.DishDTO;
import com.nebula.dto.DishPageQueryDTO;
import com.nebula.entity.Dish;
import com.nebula.result.PageResult;
import com.nebula.result.Result;
import com.nebula.service.DishService;
import com.nebula.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/dish")
@Api( tags = "菜品相关接口")
public class DishController {
    @Autowired
    private DishService dishService;

//    @Autowired
//    private RedisTemplate redisTemplate;

    @PostMapping
    @ApiOperation("新增菜品")
    @CacheEvict(cacheNames = "dishCache",key = "#dishDTO.categoryId")
    public Result save(@RequestBody DishDTO dishDTO){
        dishService.saveWithFlavor(dishDTO);
       // redisTemplate.delete("dish_" + dishDTO.getCategoryId());
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("菜品分夜查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        PageResult pageResult = dishService.page(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    @DeleteMapping
    @ApiOperation("根据id删除菜品")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result delete(@RequestParam List<Long> ids){
        dishService.deleteBatch(ids);
       // Set keys = redisTemplate.keys("dish_*");
       // redisTemplate.delete(keys);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> getById(@PathVariable Long id){
        DishVO dishVo = dishService.getById(id);
        return Result.success(dishVo);
    }

    @PutMapping
    @ApiOperation("修改菜品信息")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result update(@RequestBody DishVO dishVO){
      //  Set keys = redisTemplate.keys("dish_*");
      //  redisTemplate.delete(keys);
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @ApiOperation("启售停售菜品")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result StartOrStop(@PathVariable Integer status,Long id){
        dishService.startOrStop(status,id);
      //  Set keys = redisTemplate.keys("dish_*");
      //  redisTemplate.delete(keys);
        return Result.success();
    }


    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> list(Long categoryId){
        List<Dish> dishList = dishService.list(categoryId);
        return Result.success(dishList);
    }


}
